﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Leadtools.WinForms;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Design;
using Leadtools;
using Leadtools.ImageProcessing;
using SmallERP.PinBan.PB.Presenter;
using SmallERP.MODEL.PBModel;
using Smallerp.Contracts.DataContracts;

namespace Small.PinBan.PB.Comm
{

    public delegate void MyPicViewMoveHandler(PicView sender, Point newPoint);
    //public delegate void WhirlHandler(PicView sender);
    /// <summary>
    /// 用于存放图片以及相关属性的图片矩形类
    /// </summary>
    public class PicView : RasterImageViewer
    {
        public event MyPicViewMoveHandler PicMoveEvent;
        //public event WhirlHandler PicWhirlEvent;

        public bool LeftAnchor { get; set; }//左边锚上
        public bool RightAnchor { get; set; }//右边锚上
        public bool UpAnchor { get; set; }//上边锚上
        public bool DownAnchor { get; set; }//下边锚上


        //计算实际照片的宽高
        /// <summary>
        /// //像素实际坐标X
        /// </summary>
        public int PixFactX { get; set; }
        /// <summary>
        /// //像素实际坐标Y
        /// </summary>
        public int PixFactY { get; set; }
        /// <summary>
        /// //像素实际宽
        /// </summary>
        public int PixFacWidth { get {
            if (PInfo.Photo_Base_Info.PhotoId != 0)
            {
                if (PInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI) + DataTransfer.BarCodePixFactWidth;
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI) + DataTransfer.BarCodePixFactWidth;

                    }
                }
                else if (PInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);

                    }
                }
                else
                {
                    if (PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);

                    }
                }
            }
            else
            {
                return 0;
            }
        } }
        /// <summary>
        /// //像素实际高
        /// </summary>
        public int PixFacHeight { get {
            if (PInfo.Photo_Base_Info.PhotoId != 0)
            {
                if (PInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (!PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);

                    }
                }
                else if (PInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (!PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI) + DataTransfer.BarCodePixFactWidth;
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI) + DataTransfer.BarCodePixFactWidth;

                    }
                }
                else
                {
                    if (!PInfo.IsWhirl)
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);
                    }
                    else
                    {
                        return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI);

                    }
                }
            }
            else
            {
                return 0;
            }
        } }
        public Rectangle PixFactRect
        {
            get { return new Rectangle(PixFactX, PixFactY, PixFacWidth, PixFacHeight); }

        }


        public int PixWidthForCurrPrint {
            get { return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI); }
        }
        public int PixHeigthForCurrPrint
        {
            get { return Convert.ToInt32((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI * DataTransfer.CurPrinerInfo.PDPI); }
        }

        public Rectangle Rect// 此图形的矩形对像
        {
            get
            {
                return new Rectangle(x, y, Width, Height);
            }
        }

        public bool IsOutRedLine//是否在红线外
        {
            get
            {
                if (Left >= DataTransfer.ScreenBoardLength)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool IsOverRedLin//是否压线红
        {
            get
            {
                if (Left <= DataTransfer.ScreenBoardLength && Right > DataTransfer.ScreenBoardLength)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        
        public Label _lblPName;//用来显示照片的名称
        public Label _lblPSize;//用来显示照片的尺寸
        public int Area { get { return Width * Height; } }//“面积”指在屏幕上以相素点为单位的面积
        public PhotoInfo_PBDCT PInfo { get; set; }
        public int x { get { return base.Location.X; } }//左上角的X坐标
        public int y { get { return base.Location.Y; } }//左上角的Y坐标

        private bool _isWhril = false;
        public bool IsWhirl { get { return _isWhril; } set { _isWhril = value; PInfo.IsWhirl = value; WhirlPic(); } }//是否旋转,默认为false

        public string PicName { get { return "pic" + PInfo.PhotoId.ToString(); } }

        public new Point Location { get { return base.Location; } set { base.Location = value; PInfo.Location = value; } }
        public new Point PixLocation { get { return new Point(this.PixFactX, this.PixFactY); } }
        public Point Point1 { get { return new Point(x, y); } }//左上
        public Point Point2 { get { return new Point(x + base.Width, y); } }//右上
        public Point Point3 { get { return new Point(x + base.Width, y + base.Height); } }//右下
        public Point Point4 { get { return new Point(x, y + base.Height); } }//左下

        public Point PixPoint1 { get { return new Point(PixFactX, PixFactY); } }//左上
        public Point PixPoint2 { get { return new Point(PixFactX + PixFacWidth, PixFactY); } }//右上
        public Point PixPoint3 { get { return new Point(PixFactX + PixFacWidth, PixFactY); } }//右下
        public Point PixPoint4 { get { return new Point(PixFactX, PixFactY+PixFacHeight); } }//左下

        public int Type { get; set; }//0：默认的为正常的图片，1：为多选的图片集合,2：代表被框选的国片

        /// <summary>
        /// 宽，单位：屏幕上的像素
        /// </summary>
        public new int Width
        {
            get { return base.Width; }
            set
            {
                base.Width = value;
            }
        }
        /// <summary>
        /// 高，单位：屏幕上的像素
        /// </summary>
        public new int Height
        {
            get { return base.Height; }
            set
            {
                base.Height = value;
            }
        }


        private const int SPI_GETDRAGFULLWINDOWS = 38;
        private const int SPI_SETDRAGFULLWINDOWS = 37;
        private const int SPIF_SENDWININICHANGE = 2;
        private const int SPIF_UPDATEINIFILE = 1;

        public const int WM_MOVING = 0x0216;
        public const int WM_MOVE = 0x0003;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOVING:
                    {
                        DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT rect = (DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT)Marshal.PtrToStructure(m.LParam, typeof(DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT));
                        if (PicMoveEvent != null)
                        {
                            this.Cursor = Cursors.Hand;
                            PicMoveEvent(this, rect.ToRectangle().Location);
                        }

                        break;
                    }
                case WM_MOVE:
                    {
                        this.Cursor = Cursors.Default;
                        break;
                    }
            }
            base.WndProc(ref m);
        }

        RasterImage _curImg = null;//保存当前的图片

        public PicView(PhotoInfo_PBDCT pinfo, Point locatPoint)
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            //this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true); 
            PInfo = pinfo;
            PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode;//黙认无条码
            Location = locatPoint;
            //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
            //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
            Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
            Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
            
            if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
            {
                Width += DataTransfer.BarCodePixWidth;
                PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft;

              
            }
            else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
            {
                Height += DataTransfer.BarCodePixWidth;
                PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom;
            }
            else
            {
                PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode;
            }


            _isWhril = pinfo.IsWhirl;
            if (_isWhril)
            {
                int newWidth, newHeight;
                if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)//有条码旋转，并且条码在左右
                {
                    newWidth = Height + DataTransfer.BarCodePixWidth;
                    newHeight = Width - DataTransfer.BarCodePixWidth;

                 
                }
                else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom) // 有条码旋转,并表条码在上下
                {
                    newWidth = Height - DataTransfer.BarCodePixWidth;
                    newHeight = Width + DataTransfer.BarCodePixWidth;

                   
                }
                else//无条码
                {
                    newWidth = Height;
                    newHeight = Width;

                 
                }

                Width = newWidth;
                Height = newHeight;
            }
            else
            {
               
                //if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)//有条码旋转，并且条码在左右
                //{

                  
                //}
                //else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom) // 有条码旋转,并表条码在上下
                //{

                //    PixFacWidth = PixWidthForCurrPrint;
                //    PixFacHeight = PixHeigthForCurrPrint + DataTransfer.BarCodePixFactWidth;
                //}
                //else//无条码
                //{

                //    PixFacWidth = PixWidthForCurrPrint;
                //    PixFacHeight = PixHeigthForCurrPrint;
                //}
            }


            base.AutoScroll = false;
            base.BorderStyle = System.Windows.Forms.BorderStyle.None;
            base.FrameColor = Color.YellowGreen;
            base.SizeMode = Leadtools.RasterPaintSizeMode.Stretch;

            //照片ID
            _lblPName = new Label();
            _lblPName.Text = "Pic" + PInfo.PhotoId.ToString();
            _lblPName.Location = new Point(1, 1);
            _lblPName.AutoSize = true;
            _lblPName.BackColor = Color.Transparent;
            _lblPName.ForeColor = Color.Red;
            _lblPName.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPName.BringToFront();
            base.Controls.Add(_lblPName);

            //照片尺寸
            _lblPSize = new Label();
            _lblPSize.Text = PInfo.PhotoSize;
            _lblPSize.AutoSize = true;
            _lblPSize.BackColor = Color.Transparent;
            _lblPSize.ForeColor = Color.Red;
            _lblPSize.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPSize.BringToFront();
            _lblPSize.Location = new Point(1, 10);
            base.Controls.Add(_lblPSize);

            IilitialImg();

            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);
        }
        public PicView(PhotoInfo_PBDCT pinfo, Point locatPoint, bool isNew)
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            //this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true); 
            PInfo = pinfo;
            //PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode;//黙认无条码
            Location = locatPoint;
            //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
            //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);

            //if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
            //{
            //    Width += DataTransfer.BarCodePixWidth;
            //    PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft;
            //}
            //else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
            //{
            //    Height += DataTransfer.BarCodePixWidth;
            //    PInfo.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom;
            //}


            //_isWhril = pinfo.IsWhirl;
            //if (_isWhril)
            //{
            //    int newWidth, newHeight;
            //    if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)//有条码旋转，并且条码在左右
            //    {
            //        newWidth = Height + DataTransfer.BarCodePixWidth;
            //        newHeight = Width - DataTransfer.BarCodePixWidth;
            //    }
            //    else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom) // 有条码旋转,并表条码在上下
            //    {
            //        newWidth = Height - DataTransfer.BarCodePixWidth;
            //        newHeight = Width + DataTransfer.BarCodePixWidth;
            //    }
            //    else//无条码
            //    {
            //        newWidth = Height;
            //        newHeight = Width;
            //    }

            //    Width = newWidth;
            //    Height = newHeight;
            //}
            if (pinfo.IsWhirl)
            {
                if (pinfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Width += 1;

                  
                }
                else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Height += 1;

                  
                }
                else
                {
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                   
                }

            }
            else
            {
                if (pinfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Width += 1;

                  
                }
                else if (DataTransfer.curTemplateInfo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Height += 1;
               
                }
                else
                {
                    //Width = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    //Height = Convert.ToInt32(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL);
                    Width = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixWidth / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                    Height = Convert.ToInt32(Math.Ceiling(((double)PInfo.Photo_Base_Info.PixHeight / PInfo.Photo_Base_Info.DPI) * DataTransfer.PERPIXEL));
                   
                }
            }

            base.AutoScroll = false;
            base.BorderStyle = System.Windows.Forms.BorderStyle.None;
            base.FrameColor = Color.YellowGreen;
            base.SizeMode = Leadtools.RasterPaintSizeMode.Stretch;

            //照片ID
            _lblPName = new Label();
            _lblPName.Text = "Pic" + PInfo.PhotoId.ToString();
            _lblPName.Location = new Point(1, 1);
            _lblPName.AutoSize = true;
            _lblPName.BackColor = Color.Transparent;
            _lblPName.ForeColor = Color.Red;
            _lblPName.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPName.BringToFront();
            base.Controls.Add(_lblPName);

            //照片尺寸
            _lblPSize = new Label();
            _lblPSize.Text = PInfo.PhotoSize;
            _lblPSize.AutoSize = true;
            _lblPSize.BackColor = Color.Transparent;
            _lblPSize.ForeColor = Color.Red;
            _lblPSize.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPSize.BringToFront();
            _lblPSize.Location = new Point(1, 10);
            base.Controls.Add(_lblPSize);

            IilitialImg();

            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);
        }
        
        public void IilitialImg()
        {
            if (DataTransfer.IsShowImg)
            {
                try
                {
                    if (_curImg == null)
                    {
                        _curImg = CommonUtil.GetStampPic(PInfo.PicPath);
                    }

                    if (_curImg != null)
                    {
                        RotatPic(_curImg);
                    }


                    base.Image = _curImg;
                }
                catch (Exception)
                {
                    System.Windows.Forms.MessageBox.Show("没有找到图片，请检查图片是否被移走/r/n" + PInfo.PicPath);
                }
            }
            else
            {
                if (base.Image != null)
                {
                    _curImg = base.Image.CloneAll();
                }

                base.Image = null;
            }
        }

        private void WhirlPic()//旋转图片
        {
            if (DataTransfer.IsShowImg)
            {
                if (_curImg != null)
                {
                    RotatPic(_curImg);
                    base.Image = _curImg;
                }
            }
        }

        private bool isWhrilIng = false;
        private void RotatPic(RasterImage img)
        {
            RotateCommand command = new RotateCommand();
            command.Flags = RotateCommandFlags.Resize;
            if (_isWhril)
            {
                if (!isWhrilIng)
                {
                    command.Angle = -90 * 100;
                    command.Run(img);
                    isWhrilIng = true;
                }

            }
            else
            {
                if (isWhrilIng)
                {
                    command.Angle = 90 * 100;
                    command.Run(img);
                    isWhrilIng = false;
                }
            }

        }

        public PicView(int photoid, int width, int height, Point locatPoint)//用于构建零时的图片，主要用于虚线框图片（包括移动、或是旋转）用来和其它图片验证是否重叠等用途
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            //this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true); 
            PInfo = new PhotoInfo_PBDCT();

            PInfo.Photo_Base_Info.PhotoId = photoid;
            Width = width;
            Height = height;
            Location = locatPoint;

            base.AutoScroll = false;
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

            //GetSuitPoint(this);
        }
        public PicView(PhotoInfo_PBDCT pinfo, int width, int height, Point locatPoint)//用于构建零时的图片，主要用于虚线框图片（包括移动、或是旋转）用来和其它图片验证是否重叠等用途
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            //this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true); 
            PInfo = new PhotoInfo_PBDCT();

            PInfo = pinfo;
            Width = width;
            Height = height;
            Location = locatPoint;

            base.AutoScroll = false;
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

            //GetSuitPoint(this);
        }
        /// <summary>
        /// 当条码的方向变化时
        /// </summary>
        /// <param name="curStatus"></param>
        void PInfo_BarCodeStatusChangedEvent(SmallERP.COM.Status.PhotoBarCodeStatus curStatus)
        {
            //throw new NotImplementedException();
        }

        void PicView_Leave(object sender, EventArgs e)
        {
            BoardColor = Color.Black;
            //this.Paint -= new PaintEventHandler(PicView_Paint);

        }

        void PicView_Enter(object sender, EventArgs e)
        {

            BoardColor = Color.Red;
            //this.Paint += new PaintEventHandler(PicView_Paint);


        }

        Color BoardColor = Color.Black;

        Point pStart;
        Point pEnd;
        void PicView_Paint(object sender, PaintEventArgs e)
        {
            ButtonBorderStyle bs = ButtonBorderStyle.Solid;
            if (Type != 0)
            {
                bs = ButtonBorderStyle.Dotted;
                BoardColor = Color.Red;
            }

            ControlPaint.DrawBorder(e.Graphics,
                        base.ClientRectangle,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs);
            if (Type != 1)
            {
                switch (PInfo.BarCodeStatus)
                {
                    case SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft:
                        pStart = new Point(2, 5);
                        pEnd = new Point(2, this.Height - 5);
                        DrawALine(e.Graphics, Color.Tomato, pStart, pEnd);
                        break;
                    case SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom:
                        pStart = new Point(5, 2);
                        pEnd = new Point(this.Width - 5, 2);
                        DrawALine(e.Graphics, Color.Tomato, pStart, pEnd);
                        break;
                    case SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode:
                        break;
                    default:
                        break;
                }
            }
        }

        public void DrawALine(Graphics g, Color color, Point pointStart, Point pointEnd)
        {

            Pen p = new Pen(color, 2);
            g.DrawLine(p, pointStart, pointEnd);
        }

        /// <summary>
        /// 框选多图专用
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="locatPoint"></param>
        /// <param name="type"></param>
        public PicView(int width, int height, Point locatPoint)//框选多图专用
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            //this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true); 
            PInfo = new PhotoInfo_PBDCT();
            PInfo.Photo_Base_Info.PhotoId = 0;
            Width = width;
            Height = height;
            Location = locatPoint;


            base.AutoScroll = false;
            base.BorderStyle = System.Windows.Forms.BorderStyle.None;
            base.FrameColor = Color.YellowGreen;
            //base.SizeMode = Leadtools.RasterPaintSizeMode.Stretch;

            this.Type = 1;//代表多框选
            //BorderStyle = BorderStyle.FixedSingle;
            //base.AutoScroll = false;
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

        }

        private void GetSuitPoint(PicView pv)
        {
            Point p = pv.Location;
            if (p.X < 0)
            {
                p.X = 0;
            }
            if (pv.Location.Y < 0)
            {
                p.Y = 0;
            }
            if (p.Y + pv.Height > DataTransfer.ScreenBoardHeight)
            {
                p.Y = DataTransfer.ScreenBoardHeight - pv.Height;
            }
            if (Right > DataTransfer.ScreenBoardLength && Left <= DataTransfer.ScreenBoardLength)//判断是否压红线
            {
                p.X = DataTransfer.ScreenBoardLength + 10;
            }

            pv.Location = p;
        }
    }
}
